package vben.base.sys.perm.role;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vben.common.core.utils.IdUtils;
import vben.common.jdbc.dto.PageData;
import vben.common.jdbc.sqler.Sqler;
import vben.common.redis.utils.RedisUtils;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class SysPermRoleService {

    private final SysPermRoleDao dao;

    @Transactional(readOnly = true)
    public PageData findPageData(Sqler sqler) {
        return dao.findPageData(sqler);
    }

    public SysPermRole findById(Long id) {
        return dao.findById(id);
    }

    public List<MenuVo> findMenuVoList() {
        return dao.findMenuVoList();
    }

    public void insert(SysPermRole main) {
        main.setId(IdUtils.getSnowflakeNextId());
        dao.insert(main);
        clearCache();
    }

    public void update(SysPermRole main) {
        dao.update(main);
        clearCache();
    }

    public int delete(Long[] ids) {
        for (Long id : ids) {
            dao.deleteById(id);
        }
        clearCache();
        return ids.length;
    }

    public void clearCache() {
        RedisUtils.deleteKeys("rlist:*");
        RedisUtils.deleteKeys("oids:*");
    }
}
